package com.carSys.Service;

import com.carSys.DTO.VehicleModelStatisticsDTO;
import com.carSys.Enums.TaskType;
import com.carSys.Mapper.VehicleMapper;
import com.carSys.Mapper.VehicleModelStatisticsDTOMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VehicleModelStatisticsDTOService {

    @Autowired
    private VehicleModelStatisticsDTOMapper vehicleModelStatisticsDTOMapper;

    @Autowired
    private VehicleMapper vehicleMapper;


    public List<VehicleModelStatisticsDTO> getAllVehicleModelStatistics() {
        List<String> allVehicleModels = vehicleMapper.getAllVehicleModels();
        List<VehicleModelStatisticsDTO> vehicleModelStatisticsDTOList = new ArrayList<>();
        for(String vehicleModel : allVehicleModels) {
           long repairedTimes =  vehicleModelStatisticsDTOMapper.getRepairedTimes(vehicleModel);
           double averageRepairCost = vehicleModelStatisticsDTOMapper.getAverageRepairCost(vehicleModel);
           double repairTimeFrequency = vehicleModelStatisticsDTOMapper.getRepairTimeFrequency(vehicleModel);
              List<TaskType> mostRiskType = vehicleModelStatisticsDTOMapper.getMostRiskType(vehicleModel);
            String riskTypeNames = mostRiskType == null || mostRiskType.isEmpty()
                    ? ""
                    : mostRiskType.stream()
                    .map(TaskType::name)  // 提取枚举的名称（如HIGH_RISK）
                    .collect(Collectors.joining(","));  // 用逗号拼接
            VehicleModelStatisticsDTO vehicleModelStatisticsDTO = new VehicleModelStatisticsDTO(vehicleModel,repairedTimes,averageRepairCost,repairTimeFrequency,riskTypeNames);
            vehicleModelStatisticsDTOList.add(vehicleModelStatisticsDTO);
        }
        return vehicleModelStatisticsDTOList;
    }
}
